Skip to content

Method: appendNewNodes(SimpleListValueDescriptor, ListHandler.MergeResult)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
18: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
19: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
20: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import org.openrdf.model.Resource;
23: import org.openrdf.model.Statement;
24: import org.openrdf.model.URI;
25: import org.openrdf.model.ValueFactory;
26:
27: import java.util.ArrayList;
28: import java.util.Collection;
29: import java.util.Iterator;
30: import java.util.List;
31:
32: class SimpleListHandler extends ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> {
33:
34: SimpleListHandler(Connector connector, ValueFactory vf) {
35: super(connector, vf);
36: }
37:
38: @Override
39: SesameIterator createIterator(SimpleListDescriptor listDescriptor) throws SesameDriverException {
40: return new SimpleListIterator(listDescriptor, connector, vf);
41: }
42:
43: URI createListHead(SimpleListValueDescriptor listValueDescriptor, Collection<Statement> listStatements) {
44: final URI firstNode = sesameUri(listValueDescriptor.getValues().get(0).getIdentifier());
45: listStatements.add(vf.createStatement(owner(listValueDescriptor), hasList(listValueDescriptor),
46: firstNode, context(listValueDescriptor)));
47: return firstNode;
48: }
49:
50: List<Statement> createListRest(URI head, SimpleListValueDescriptor listValueDescriptor) {
51: final List<Statement> statements = new ArrayList<>(listValueDescriptor.getValues().size());
52: URI previous = head;
53: final URI nextNodeProp = hasNext(listValueDescriptor);
54: final URI context = context(listValueDescriptor);
55: final Iterator<NamedResource> it = listValueDescriptor.getValues().iterator();
56: it.next();
57: while (it.hasNext()) {
58: final URI object = sesameUri(it.next().getIdentifier());
59: statements.add(vf.createStatement(previous, nextNodeProp, object, context));
60: previous = object;
61: }
62: return statements;
63: }
64:
65: /**
66: * We are using this code instead of iterator.remove for performance
67: * reasons. The iterator has to reconnect the list for each removed node,
68: * which takes a lot of time.
69: */
70: @Override
71: void clearList(SimpleListValueDescriptor listValueDescriptor) throws SesameDriverException {
72: final URI context = context(listValueDescriptor);
73: final Collection<Statement> toRemove = new ArrayList<>();
74: URI currentProperty = hasList(listValueDescriptor);
75: final URI hasNext = hasNext(listValueDescriptor);
76: final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
77: Collection<Statement> stmts;
78: Resource subject = owner(listValueDescriptor);
79: do {
80: stmts = connector.findStatements(subject, currentProperty, null, includeInferred,
81: context);
82: if (!stmts.isEmpty()) {
83: subject = extractListNode(stmts, hasNext);
84: toRemove.addAll(stmts);
85: }
86: currentProperty = hasNext;
87: } while (!stmts.isEmpty());
88: connector.removeStatements(toRemove);
89: }
90:
91: @Override
92: MergeResult mergeWithOriginalList(SimpleListValueDescriptor listDescriptor, SesameIterator it) throws SesameDriverException {
93: int i = 0;
94: Resource node = null;
95: while (it.hasNext() && i < listDescriptor.getValues().size()) {
96: node = it.nextNode();
97: final NamedResource newNode = listDescriptor.getValues().get(i);
98: if (!node.stringValue().equals(newNode.getIdentifier().toString())) {
99: node = sesameUri(newNode.getIdentifier());
100: it.replaceCurrentWith(newNode);
101: }
102: i++;
103: }
104: return new MergeResult(i, node);
105: }
106:
107: @Override
108: void appendNewNodes(SimpleListValueDescriptor listDescriptor, MergeResult mergeResult) throws SesameDriverException {
109: int i = mergeResult.i;
110: final Collection<Statement> toAdd = new ArrayList<>(listDescriptor.getValues().size() - i);
111: Resource previous = mergeResult.previous;
112: final URI nextNode = sesameUri(listDescriptor.getNextNode().getIdentifier());
113: final URI context = context(listDescriptor);
114:• while (i < listDescriptor.getValues().size()) {
115: final Resource newNode = sesameUri(listDescriptor.getValues().get(i).getIdentifier());
116: final Statement stmt = vf.createStatement(previous,
117: nextNode, newNode, context);
118: toAdd.add(stmt);
119: previous = newNode;
120: i++;
121: }
122: connector.addStatements(toAdd);
123: }
124:
125: @Override
126: SesameIterator iterator(SimpleListValueDescriptor listDescriptor) throws SesameDriverException {
127: return new SimpleListIterator(listDescriptor, connector, vf);
128: }
129: }